<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title=" '编辑商品'" width="900px">
    <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="80px">
      <el-form-item label="商品名称">
        <el-input v-model="form.goodsName" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="供价">
        <el-input v-model="form.costPrice" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="售价">
        <el-input v-model="form.sellPrice" style="width: 320px;" />
      </el-form-item>
      <el-form-item label="库存">
        <el-input v-model="form.inventory" style="width: 320px;" />
      </el-form-item>
      <el-form-item label="商品状态">
        <el-radio v-model="form.status" :label="0">已上架</el-radio>
        <el-radio v-model="form.status" :label="1">已下架</el-radio>
        <el-radio v-model="form.status" :label="2">已删除</el-radio>
      </el-form-item>
    
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getCates } from '@/api/StoreCategory'
import { add, edit } from '@/api/StoreProduct'
import editor from '../../components/Editor'
import picUpload from '@/components/pic-upload'
import mulpicUpload from '@/components/mul-pic-upload'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import MaterialList from '@/components/material'
import Cookies from 'js-cookie'
export default {
  components: { editor, picUpload, mulpicUpload, Treeselect, MaterialList },
  data() {
    return {
      loading: false, dialog: false, cates: [],
      dialogFormVisible:false,
      form: {
        id: '',
        status:2,
        productId:'',
        isDelete:'',
        shopId:0,
        goodsName:'',
        costPrice:0,
        sellPrice:0,
        inventory:0,
        sliderImage: '',
        imageArr: [],
        sliderImageArr: [],
        bak1:'',
        bak2:'',
      },
      rules: {
      }
    }
  },
  watch: {
    'form.imageArr': function(val) {
      if (val) {
        this.form.image = val.join(',')
      }
    },
    'form.sliderImageArr': function(val) {
      if (val) {
        this.form.sliderImage = val.join(',')
      }
    }
  },
  // mounted: {
  //   initDomains()
  // },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
       this.doEdit()
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.queryList()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        status:2,
        shopId:0,
        sliderImage: '',
        imageArr: [],
        sliderImageArr: [],
        storeCategory: {id:null},
        isShow: 1,
        isHot: 0,
        isBenefit: 0,
        isBest: 0,
        isNew: 0,
        productName: '',
        goodsList:[ { goodsName: '', costPrice:0,sellPrice: 0,inventory:0,status:1,show:true}],
        bak1:'',
        bak2:'',
        subtitle: ''
      }
    },
    getCates() {
      getCates({ enabled: true }).then(res => {
        this.cates = res.content
      })
    }
  }
}
</script>

<style scoped>

</style>
